﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace RichFrame
{
    /// <summary>
    /// 单例
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class MonoSingleton<T> : MonoBehaviour where T : MonoBehaviour
    {
        public static T Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = FindObjectOfType<T>();
                    if(_Instance == null)
                    {
                        GameObject gameObject = new GameObject();
                        gameObject.name = typeof(T).Name;
                        _Instance = gameObject.AddComponent<T>();
                    }    
                }
                return _Instance;
            }
        }
        static T _Instance;
    }
}
